package cn.miao.hrm.controller;

import cn.miao.hrm.service.ICourseTypeService;
import cn.miao.hrm.domain.CourseType;
import cn.miao.hrm.query.CourseTypeQuery;
import cn.miao.util.AjaxResult;
import cn.miao.util.PageList;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/courseType")
public class CourseTypeController {
    @Autowired
    public ICourseTypeService courseTypeService;



    //查询课程分类面包屑
    @RequestMapping(value = "/crumbs/{id}", method = RequestMethod.GET)
    public AjaxResult crumbs(@PathVariable("id") Long id) {
        return courseTypeService.crumbs(id);
    }
    /**
     * 树性数据,获取三级课程类型
     *
     * @return {@link AjaxResult}
     */
    @GetMapping("/treeData")
    public AjaxResult treeData(){
        return AjaxResult.me().setResultObj(courseTypeService.getTreeData());
    }
    /**
    * 保存和修改
    */
    @RequestMapping(value="/save",method= RequestMethod.POST)
    public AjaxResult save(@RequestBody CourseType courseType){
        if(courseType.getId()!=null){
            courseTypeService.updateById(courseType);
        }else{
            courseTypeService.insert(courseType);
        }
        return AjaxResult.me();
    }

    /**
    * 删除对象
    */
    @RequestMapping(value="/{id}",method=RequestMethod.DELETE)
    public AjaxResult delete(@PathVariable("id") Long id){
        courseTypeService.deleteById(id);
        return AjaxResult.me();
    }

   /**
   * 根据ID获取对象
   */
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public AjaxResult get(@PathVariable("id")Long id){
        return AjaxResult.me().setResultObj(courseTypeService.selectById(id));
    }


    /**
    * 获取所有对象
    */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public AjaxResult list(){
        return AjaxResult.me().setResultObj(courseTypeService.selectList(null));
    }

    /**
    * 分页查询数据
    */
    @RequestMapping(value = "/pagelist",method = RequestMethod.POST)
    @PreAuthorize("hasAnyAuthority('course:pagelist')")
    public AjaxResult json(@RequestBody CourseTypeQuery query){
        Page<CourseType> page = new Page<CourseType>(query.getPage(),query.getRows());
        page = courseTypeService.selectPage(page);
        PageList pageList = new PageList<CourseType>(page.getTotal(),page.getRecords());
        return AjaxResult.me().setResultObj(pageList);
    }
}
